jQuery(document).ready(function($) {

    //Mobile menu
    $('#dl-menu').dlmenu({
        animationClasses: {classin: 'dl-animate-in-2', classout: 'dl-animate-out-2'}
    });

    //Portfolio sort
    $('.row').mixitup({
        effects: ['fade', 'rotateZ'],
        easing: 'smooth'
    });

    //Form validation
    $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
        filter: function() {
            return $(this).is(":visible");
        }
    });

    //Google map
    $('.google-map').gMap({address: 'Los Angeles, CA',
        panControl: true,
        zoomControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL
        },
        mapTypeControl: true,
        scaleControl: true,
        streetViewControl: true,
        overviewMapControl: true,
        scrollwheel: true,
        icon: {
            image: "http://www.google.com/mapfiles/marker.png",
            shadow: "http://www.google.com/mapfiles/shadow50.png",
            iconsize: [20, 34],
            shadowsize: [37, 34],
            iconanchor: [9, 34],
            shadowanchor: [19, 34]
        },
        zoom: 15,
        markers: [
            {'address': 'Los Angeles, CA'}
        ]

    });

    // prettyPhoto
    $("a[data-rel^='prettyPhoto']").prettyPhoto({
        animation_speed: 'normal',
        slideshow: 3000,
        hook: 'data-rel'
    });

    //Slider
    $('.flexslider').flexslider({
        animation: "fade",
        prevText: "",
        nextText: "",
        controlNav: false,
        start: function() {
            $('.flex-caption').addClass("slideUp");
            $(".flex-caption").css({"opacity": "1"});
        },
        before: function() {
            setTimeout(function() {
                $('.flex-caption').addClass("slideUp");

            }, 800);
            $(".flex-caption").css({"opacity": "0"});
        },
        after: function() {
            $('.flex-caption').removeClass("slideUp");
            $(".flex-caption").css({"opacity": "1"});
        }
    });

    //Header animation
    var $head = $('.header');
    $('.waypoint').each(function(i) {
        var $el = $(this),
                animClassDown = $el.data('animateDown'),
                animClassUp = $el.data('animateUp');

        $el.waypoint(function(direction) {
            if (direction === 'down' && animClassDown) {
                $head.attr('class', 'header ' + animClassDown);
            }
            else if (direction === 'up' && animClassUp) {
                $head.attr('class', 'header ' + animClassUp);
            }
        }, {offset: '100%'});
    });

    $(window).scroll(function() {

        //Service
        $('.service').each(function(i, el) {
            var elementPos = $(this).offset().top;

            var topOfWindow = $(window).scrollTop();

            if (elementPos < topOfWindow + 400) {

                setTimeout(function() {
                    $(el).addClass("fadeIn");
                }, 100 + (i * 150));
            }
        });

        //Project
        $('.project').each(function(i, el) {
            var elementPos = $(this).offset().top;

            var topOfWindow = $(window).scrollTop();

            if (elementPos < topOfWindow + 600) {

                setTimeout(function() {
                    $(el).addClass("fadeIn");
                }, 100 + (i * 150));
            }
        });

        //Progress bar
        $('.progress .bar').each(function(i, el) {
            var elementPos = $(this).offset().top;

            var topOfWindow = $(window).scrollTop();

            if (elementPos < topOfWindow + 1000) {

                setTimeout(function() {
                    $(el).addClass("stretchRight");
                }, 100 + (i * 150));
            }
        });


        $('.google-map').each(function() {
            var elementPos = $(this).offset().top;

            var topOfWindow = $(window).scrollTop();

            if (elementPos < topOfWindow + 400) {
                $(this).addClass("slideExpandUp");
            }
        });

    });

    //Scroll
    $('nav a').on('click', function() {

        var scrollAnchor = $(this).attr('data-scroll'),
                scrollPoint = $('section[data-anchor="' + scrollAnchor + '"]').offset().top - $(this).attr("data-anchor-offset");
        $('body,html').animate({
            scrollTop: scrollPoint
        }, 500);

        return false;

    });

    $(window).scroll(function() {
        var windscroll = $(window).scrollTop();
        if (windscroll >= 100) {
            $('section').each(function(i) {
                if ($(this).position().top <= windscroll + 240) {
                    $('ul.main-menu li.active-main').removeClass('active-main');
                    $('ul.main-menu li').eq(i).addClass('active-main');
                }
            });

        } else {
            $('ul.main-menu li.active-main').removeClass('active-main');
            $('ul.main-menu li:first').addClass('active-main');
        }

    }).scroll();

});